package com.xin.di.uav.applet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xin.di.uav.applet.mapper.ServiceAreaMapper;
import com.xin.di.uav.applet.request.ServiceAreaAddParam;
import com.xin.di.uav.applet.request.ServiceAreaEditParam;
import com.xin.di.uav.common.response.IdAndNameResp;
import com.xin.di.uav.applet.resp.ServiceAreaResp;
import com.xin.di.uav.applet.service.ServiceAreaService;
import com.xin.di.uav.common.exception.BusinessException;
import com.xin.di.uav.common.request.common.IdRequest;
import com.xin.di.uav.common.utils.BeanUtil;
import com.xin.di.uav.common.utils.PhoneDesensitizationUtil;
import com.xin.di.uav.operate.mapper.CustomDeliveryPointMapper;
import com.xin.di.uav.operate.po.ServiceAreaPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class ServiceAreaServiceImpl implements ServiceAreaService {
    @Autowired
    private ServiceAreaMapper serviceAreaMapper;

    @Autowired
    private CustomDeliveryPointMapper customDeliveryPointMapper;

    @Override
    public boolean add(ServiceAreaAddParam serviceArea) {
        ServiceAreaPO serviceAreaPO = BeanUtil.copyProperties(serviceArea, ServiceAreaPO.class);
        serviceAreaPO.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        return serviceAreaMapper.insert(serviceAreaPO) > 0;
    }

    @Override
    public boolean delete(String id) {
        return serviceAreaMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(ServiceAreaEditParam serviceArea) {
        String id = serviceArea.getId();
        ServiceAreaPO serviceAreaPO = serviceAreaMapper.selectById(id);
        if (serviceAreaPO == null) {
            throw new BusinessException("记录不存在");
        }
        serviceAreaPO = BeanUtil.copyProperties(serviceArea, ServiceAreaPO.class);
        return serviceAreaMapper.updateById(serviceAreaPO) > 0;
    }

    @Override
    public boolean updateStatus(IdRequest idRequest) {
        String id = idRequest.getId();
        ServiceAreaPO serviceAreaPO = serviceAreaMapper.selectById(id);
        if (serviceAreaPO == null) {
            throw new BusinessException("记录不存在");
        }
        int status = serviceAreaPO.getStatus();
        if (status == 0) {
            serviceAreaPO.setStatus(1);
        } else {
            serviceAreaPO.setStatus(0);
        }
        return serviceAreaMapper.updateById(serviceAreaPO) > 0;
    }

    @Override
    public ServiceAreaPO getById(String id) {
        return serviceAreaMapper.selectById(id);
    }

    @Override
    public ServiceAreaResp list(int pageNum, int pageSize, String serviceRegion) {
        pageNum = (pageNum - 1) * pageSize;
        ServiceAreaResp serviceAreaResp = new ServiceAreaResp();
        int cnt = serviceAreaMapper.count(serviceRegion);
        if (cnt > 0) {
            serviceAreaResp.setTotal(cnt);
            List<ServiceAreaPO> serviceAreaPOS = serviceAreaMapper.selectList(serviceRegion, pageNum, pageSize);
            List<ServiceAreaResp.Record> recordList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(serviceAreaPOS)) {
                List<String> ids = serviceAreaPOS.stream().map(ServiceAreaPO::getId).collect(Collectors.toList());
                List<IdAndNameResp> idAndNameRespList = customDeliveryPointMapper.selIdAndNameResp(ids);
                for (ServiceAreaPO serviceAreaPO : serviceAreaPOS) {
                    ServiceAreaResp.Record record = BeanUtil.copyProperties(serviceAreaPO, ServiceAreaResp.Record.class);
                    String contactNumber = record.getPhone();
                    if (ObjectUtil.isNotEmpty(contactNumber) && contactNumber.length() == 11) {
                        record.setPhone(PhoneDesensitizationUtil.phoneDesensitization(record.getPhone()));
                    }
                    String names = idAndNameRespList.stream().filter(idAndNameResp -> idAndNameResp.getId().equals(serviceAreaPO.getId())).map(IdAndNameResp::getName).collect(Collectors.joining("、"));
                    record.setDeliveryPointsNames(names);
                    recordList.add(record);
                }
            }

            serviceAreaResp.setRecordList(recordList);
        }


        return serviceAreaResp;
    }

    @Override
    public List<IdAndNameResp> getIdAndName(String serviceRegion) {
        return serviceAreaMapper.searchList(serviceRegion);
    }
}
